﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

using Fcdbas.Platform.Properties;
using Fcdbas.Platform.Enums;
using Fcdbas.WinUI.Controls;

namespace Fcdbas.WinUI.Design
{
    public partial class DataItemPropertiesEdit : DevExpress.XtraEditors.XtraForm
    {
        private DataSourceBinding oldDataSource;
        private List<DataItem> DataItems;
        public List<DataItem> SelectItems
        {
            get;
            set;
        }
        public DataItemPropertiesEdit(List<DataItem> list, DataSourceBinding editDataSource)
        {
            InitializeComponent();
            this.oldDataSource = editDataSource;
            DataItems = list;
            SelectItems = new List<DataItem>();
        }
        private void GetEntityList(DevExpress.XtraEditors.Repository.RepositoryItemComboBox upComboBox)
        {
            ITypeDiscoveryService typeDiscovery = oldDataSource.Site.GetService(typeof(ITypeDiscoveryService)) as ITypeDiscoveryService;

            if (typeDiscovery != null)
            {
                Cursor previousCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;

                try
                {
                    ICollection types = typeDiscovery.GetTypes(typeof(Fcdbas.Core.IEntity), false);
                    var sorted = from t in types.Cast<Type>()
                                 orderby t.Name
                                 select t;

                    foreach (Type type in sorted)
                    {
                        if (type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(",")) != "Fcdbas.Core" &&
                          typeof(Fcdbas.Core.IEntity).IsAssignableFrom(type))
                            upComboBox.Items.Add(type.ToString());
                    }
                }
                finally
                {
                    Cursor.Current = previousCursor;
                }
            }
        }
        private void DataItemPropertiesEdit_Load(object sender, EventArgs e)
        {
            foreach (DataItem di in DataItems)
            {
                cboColumnName.Properties.Items.Add(di.ColumnName);
            }
            this.gcItemList.DataSource = DataItems;
            foreach (GridColumn gc in this.gvItemList.Columns)
            {
                if (gc.FieldName.ToUpper() == "COLUMNNAME")
                    gc.Fixed = FixedStyle.Left;
                if (gc.FieldName.ToUpper() == "CAPTION")
                    gc.Fixed = FixedStyle.Left;
                if (gc.FieldName.ToUpper() == "UPDATEOPTION")
                {
                    DevExpress.XtraEditors.Repository.RepositoryItemComboBox upComboBox = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                    foreach (DataItemUpdate type in Enum.GetValues(typeof(DataItemUpdate)))
                    {
                        upComboBox.Items.Add(type);
                    }
                    gc.ColumnEdit = upComboBox;
                }
                if (gc.FieldName.ToUpper() == "FLAGS")
                {
                    DevExpress.XtraEditors.Repository.RepositoryItemComboBox upComboBox = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                    foreach (DataItemFlags type in Enum.GetValues(typeof(DataItemFlags)))
                    {
                        upComboBox.Items.Add(type);
                    }
                    gc.ColumnEdit = upComboBox;
                }
                if (gc.FieldName.ToUpper() == "FORMATOPTION")
                {
                    string[] m_Objects = { "Uppercase", "Lowercase", "Currency", "Percentage", "Decimal", "Integer", "Date", "DateTime", "Time", "Unformatted" };
                    DevExpress.XtraEditors.Repository.RepositoryItemComboBox upComboBox = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                    foreach (string  type in m_Objects)
                    {
                        upComboBox.Items.Add(type);
                    }
                    gc.ColumnEdit = upComboBox;
                }
                if (gc.FieldName.ToUpper() == "LOGICALPARENT")
                {
                   
                    DevExpress.XtraEditors.Repository.RepositoryItemComboBox upComboBox = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
                    GetEntityList(upComboBox);
                    gc.ColumnEdit = upComboBox;
                }
            }
            foreach (FieldType type in Enum.GetValues(typeof(FieldType)))
            {
                cmbDataType.Properties.Items.Add(type);
            }
        }
        public DataItem CurrentDataItem
        {
            get;
            set;
        }
        private void cboColumnName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DataItems == null || DataItems.Count <= 0 || string.IsNullOrEmpty(cboColumnName.SelectedText))
                return;
            CurrentDataItem = (from d in DataItems
                          where d.ColumnName == cboColumnName.SelectedText
                          select d).FirstOrDefault();
            if (CurrentDataItem == null)
                return;
            txtName.Text = CurrentDataItem.Name;
            txtCaption.Text = CurrentDataItem.Caption;
            chkSimple.Checked = CurrentDataItem.Simple;
            chkUserEditor.Checked = CurrentDataItem.UseEditor;
            chkAssist.Checked = CurrentDataItem.IsAssist;
            chkQuaryable.Checked = CurrentDataItem.Queryable;
            chkOnlyWhere.Checked = CurrentDataItem.OnlyInWhereClause;
            chkImage.Checked = CurrentDataItem.IsImage;
            chkDropdown.Checked = CurrentDataItem.PropertyChanged;
            chkIsRichtext.Checked = CurrentDataItem.IsRichEdit;
            chkIsBlob.Checked = CurrentDataItem.IsBlobEdit;
            txtParentUnit.Text = CurrentDataItem.LogicalParent;
            txtRefFunction.Text = CurrentDataItem.RefFunction;
            bteValidate.Text = CurrentDataItem.Validate;
            bteEnumMethod.Text = CurrentDataItem.EnumMethod;
            cmbFormat.Text = CurrentDataItem.FormatOption;
            cmbDataType.EditValue = CurrentDataItem.FieldType;
            
            txtCustomeFormat.Text = CurrentDataItem.CustomerFormat;
            chkRequired.Checked = CurrentDataItem.Required;
            
            bteRefercnce.Text = CurrentDataItem.Reference;
            
            rgpFlags.SelectedIndex = (int)CurrentDataItem.Flags;
            rgpUpdate.SelectedIndex = (int)CurrentDataItem.UpdateOption;
            
            
        }
        public bool IsSingle
        {
            get;
            set;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CurrentDataItem == null)
            {
                MessageBox.Show("请选择要增加的字段！");
                return;
            }
            IsSingle = true;
            CurrentDataItem.Name =txtName.Text;
            CurrentDataItem.Caption = txtCaption.Text ;
            CurrentDataItem.Simple=chkSimple.Checked ;
            CurrentDataItem.UseEditor = chkUserEditor.Checked;
            CurrentDataItem.IsAssist = chkAssist.Checked;
            CurrentDataItem.Queryable = chkQuaryable.Checked;
            CurrentDataItem.OnlyInWhereClause = chkOnlyWhere.Checked;
            CurrentDataItem.IsImage = chkImage.Checked;
            CurrentDataItem.PropertyChanged = chkDropdown.Checked;
            CurrentDataItem.IsRichEdit = chkIsRichtext.Checked;
            CurrentDataItem.IsBlobEdit = chkIsBlob.Checked;
            CurrentDataItem.LogicalParent = txtParentUnit.Text;
            CurrentDataItem.RefFunction = txtRefFunction.Text;
            CurrentDataItem.Validate = bteValidate.Text;
            CurrentDataItem.EnumMethod = bteEnumMethod.Text;
            CurrentDataItem.FormatOption = cmbFormat.Text;
            CurrentDataItem.FieldType = (FieldType)cmbDataType.EditValue;

            CurrentDataItem.Required=chkRequired.Checked ;
            CurrentDataItem.Queryable=chkQuaryable.Checked ;
            CurrentDataItem.EnumMethod=bteEnumMethod.Text;
            CurrentDataItem.Reference=bteRefercnce.Text;
            CurrentDataItem.Validate=bteValidate.Text;
            CurrentDataItem.UseEditor = chkUserEditor.Checked;
            CurrentDataItem.Simple = chkSimple.Checked;
            CurrentDataItem.OnlyInWhereClause = chkOnlyWhere.Checked;
            CurrentDataItem.IsAssist = chkAssist.Checked;
            switch (rgpFlags.SelectedIndex)
            {
                case 0:
                    CurrentDataItem.Flags = Platform.Enums.DataItemFlags.ParentKey;
                    break;
                case 1:
                    CurrentDataItem.Flags = Platform.Enums.DataItemFlags.Key;
                    break;
                default:
                    CurrentDataItem.Flags = Platform.Enums.DataItemFlags.Attribute;
                    break;

            }
            switch (rgpUpdate.SelectedIndex)
            {
                case 0:
                    CurrentDataItem.UpdateOption = Platform.Enums.DataItemUpdate.Allow;
                    break;
                case 1:
                    CurrentDataItem.UpdateOption = Platform.Enums.DataItemUpdate.AllowedIfNull;
                    break;
                default:
                    CurrentDataItem.UpdateOption = Platform.Enums.DataItemUpdate.NotAllowed;
                    break;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            if (DataItems == null)
                return;
            foreach (DataItem di in DataItems)
            {
                di.AddToForm = true;
                SelectItems.Add(di);
            }
            
            gcItemList.RefreshDataSource();
        }

        private void btnUnSelect_Click(object sender, EventArgs e)
        {
            if (DataItems == null)
                return;
            foreach (DataItem di in DataItems)
            {
                di.AddToForm = false;
            }
            SelectItems.Clear();
            gcItemList.RefreshDataSource();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            foreach (DataItem di in DataItems)
            {
                if (di.AddToForm)
                    SelectItems.Add(di);
            }
            IsSingle = false;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}